package com.algorithm.hqy;

import com.altorithm.QuickSort;

import java.util.Arrays;

/**
 * @Classname FibSearch
 * @Date 2019/9/27 14:31
 * @auther by hqy
 */
public class FibSearch {

    public static int[] getFib(int[] array) {
        if (array.length <= 2) {
            return array;
        }
        array[0] = 1;
        array[1] = 1;
        for (int i = 2; i < array.length; i++) {
            array[i] = array[i - 1] + array[i - 2];
        }
        return array;
    }


    /**
     * @param [findArray, findValue]
     * @return int
     * @Description 斐波拉契查找算法，非常类似二分查找，
     * 但是中值点的选取不再是(low+high)/2,而是一个黄金分割点，
     * 每次中值的选取都是一个黄金分割点
     */
    public static int findKey(int[] findArray, int findValue) {


        int[] fib = getFib(new int[30]);

        int low = 0;
        int high = findArray.length - 1;
        int k = 0;
        int mid = 0;

        while (findArray.length > fib[k] - 1) {
            k++;
        }

        int[] temp = Arrays.copyOf(findArray, fib[k] - 1);

        for (int i = findArray.length; i < temp.length; i++) {
            temp[i] = temp[high];
        }


        /**
         *如果中值点大于要查找的值，就去前半段找，小于就去后半段找
         */
        while (low <= high) {
            mid = low + fib[k - 1] - 1;

            if (findValue < temp[mid]) {
                high = mid - 1;
                k--;
            } else if (findValue > temp[mid]) {
                low = mid + 1;
                k -= 2;
            } else {
                if (mid <= high) {
                    return mid;
                } else {
                    return high;
                }
            }
        }
        return -1;


    }

    public static int getIndex(int maxValue) {
        int n = 0;
        while (true) {
            if (getValue(n) <= maxValue) {
                n++;
            } else {
                return n;
            }
        }
    }

    public static int getValue(int n) {
        if (n < 2) {
            return 1;
        } else {
            return getValue(n - 1) + getValue(n - 2);
        }
    }

    public static void main(String[] args) {
        int value = getValue(20);
        System.out.println(value);
    }
}
